
	*Preamble;
		# d cr
		clear 
		set more off
		clear mata
		clear matrix
		set mem 700m 
		set matsize 2000 
		set maxvar 32000
		cap log close
		# d ;

	*Set working directory;
		cd "C:\Users\Lorenzo Casaburi\Documents\InventoryCredit\3ie_report_analysis";

	*Local date of most recent databases;
		local data_date 20130625;
		
	*Local out_date;
		local out_date 20130625;
		
	*Call database;
		use dta/inventory_credit_final_data_`data_date', clear;
		
	*Prep;						
		*Standardize continuous baseline variables;		
			foreach var of varlist
						B_e_qty_oil_peak 
						B_e_qty_oil_offpeak				
						 B_e_buy_for_sale
						 B_h_CE
						 B_i_disc_0_2						 						 
						 B_k6_save_50K
						 B_u_asset_score
						 B_near_mkt						 
						 B_near_road{;
				egen mean_var=mean(`var');
				egen sd_var=sd(`var');
				replace `var'=(`var'-mean_var)/sd_var;						 
				drop mean_var sd_var;
			};
		*global B_X;
			global B_X B_e_qty_oil_peak 
						B_e_qty_oil_offpeak				
						B_e1_masanakay_any
						B_e2d_red_any
						 B_e_buy_for_sale
						 B_e_advance10
						 B_b_gender_hh_head
						 B_i_disc_0_2
						 B_j_credit_constrained
						 B_h_CE						 						 						
						 B_u_asset_score
						 B_near_mkt						 
						 B_near_road;

	**Table 4: Attrition analysis;
	local run=1;
	if `run'==1{;
		*Regression;
			estimates clear;
			foreach y in DS_sample E_sample{;				
				gen att_`y'=1-`y';
				*Reg 1: attrition vs. Treat;
					eststo: xi: reg att_`y' ICS STORAGE i.stratum_rand, cluster(communityid);
					summ att_`y' if TREAT=="Control";
					local y_mean_round=round(r(mean),.001);
					estadd local y_mean "`y_mean_round'";								
					
				*Reg 2: attrition vs. X0;
					eststo: xi: reg att_`y' ICS STORAGE $B_X i.stratum_rand, cluster(communityid);									
					summ att_`y' if TREAT=="Control";
					local y_mean_round=round(r(mean),.001);
					estadd local y_mean "`y_mean_round'";								
			};		

			*Esttab;
				esttab using "tex/t_attrition_`out_date'.tex",
				mgroups("Diary Sample" "Endline Sample" , pattern(1 0 1 0 )
				prefix(\multicolumn{@span}{c}{) suffix(})  
				span erepeat(\cmidrule(lr){@span}))
				keep(ICS  STORAGE $B_X )
				order(ICS STORAGE  $B_X )
				b(%9.3f) se(%9.3f)
				scalars("y_mean Mean Y Control Group" ) 				
				label star(* 0.1 ** 0.05 *** 0.01) replace obslast nomtitles compress longtable
				nonotes addnotes ("Notes: Robust Standard errors clustered at community level."
				"Regression includes dummies for each randomization stratum"
							"* p$<$0.1, ** p$<$0.05, *** p$<$0.01." 							)
				nogaps			
				title(Attrition \label{t-attrition});				
	};		

		
	**Table 5: Diary Storage analysis;
	local run=1;
	if `run'==1{;			
		estimates clear;
		foreach y in M_st_community_batta M_st_total D2_st_community_batta D2_st_total{;
			
			if "`y'"=="M_st_community_batta"{; local title Community Storage; };
			if "`y'"=="M_st_total"{; local title Total Storage; };
			if "`y'"=="D2_st_community_batta"{; local title Community Storage; };
			if "`y'"=="D2_st_total"{; local title Total Storage; };		
			*Regression 1;
			eststo, title(`title'): xi: reg `y' STORAGE ICS i.stratum_rand, cluster(communityid);
				summ `y' if TREAT=="Control" & e(sample);
				local y_mean_round=round(r(mean),.001);
				estadd local y_mean "`y_mean_round'";											
			*Regression 2;
			eststo, title(`title'):xi: reg `y' STORAGE ICS i.stratum_rand  $B_X , cluster(communityid);
				summ `y' if TREAT=="Control" & e(sample);;
				local y_mean_round=round(r(mean),.001);
				estadd local y_mean "`y_mean_round'";													
		};
		*Esttab;
				esttab using "tex/t_storage_`out_date'.tex",
				mgroups("July" "October" , pattern(1 0 0 0 1 0 0 0)
				prefix(\multicolumn{@span}{c}{) suffix(})  
				span erepeat(\cmidrule(lr){@span}))
				keep(ICS  STORAGE $B_X )
				order(ICS STORAGE  $B_X )
				b(%9.3f) se(%9.3f)
				scalars("y_mean Mean Y Control Group" ) 				
				label star(* 0.1 ** 0.05 *** 0.01) replace obslast mtitles compress longtable
				nonotes addnotes ("Notes: Robust Standard errors clustered at community level."
				
				"Regression includes dummies for each randomization stratum and individual covariates report in Table 4."
							"* p$<$0.1, ** p$<$0.05, *** p$<$0.01." 							)
				nogaps			
				title(Palm Oil Storage \label{t-storage});					
	};	



	**Table 6 and 6bis  Diary Panel analysis: frac_sales (with heterogeneity);
	local run=1;
	if `run'==1{;
			*Create interactions with each x in B_X;
				foreach var of varlist $B_X{;
					cap gen `var'_IC=`var'*IC;
					cap gen `var'_STORAGE=`var'*STORAGE;
					local label_var: variable label `var';
					label var `var'_IC "$\quad \quad$*IC";
					label var `var'_STORAGE "$\quad \quad$*STORAGE";					
				};	
	
		*Regs ITT;
			*Frac sale;
				estimates clear;
				foreach y in DS_frac_sale_by_jun DS_frac_sale_by_sep {;
					*Reg 1;
						eststo:xi: reg `y' STORAGE ICS i.stratum_rand  , cluster(communityid); 
						summ `y' if TREAT=="Control" & e(sample);
						local y_mean_round=round(r(mean),.001);
						estadd local y_mean "`y_mean_round'";											

					
					*reg 2;
						eststo: xi: reg `y' STORAGE ICS  i.stratum_rand $B_X   , cluster(communityid); 														
						summ `y' if TREAT=="Control" & e(sample);
						local y_mean_round=round(r(mean),.001);
						estadd local y_mean "`y_mean_round'";											
				};
				*Esttab;
					esttab using "tex/t_fracsale_`out_date'.tex",
					mgroups("By July" "By October" , pattern(1 0 1 0 )
					prefix(\multicolumn{@span}{c}{) suffix(})  
					span erepeat(\cmidrule(lr){@span}))
					keep(ICS  STORAGE $B_X )
					order(ICS STORAGE  $B_X )
					b(%9.3f) se(%9.3f)
					scalars("y_mean Mean Y Control Group"  ) 				
					label star(* 0.1 ** 0.05 *** 0.01) replace obslast nomtitles compress longtable
					nonotes addnotes ("Notes: Robust Standard errors clustered at community level."		
					"Regression includes dummies for each randomization stratum"
								"* p$<$0.1, ** p$<$0.05, *** p$<$0.01." 							)
					nogaps			
					title(Palm Oil Sale Shares \label{t-fracsale});				


		*Regs Heterogeneity;			
			gen x_IC=.;
			gen x_STORAGE=.;
			label var x_IC "$\quad \quad$*IC";
			label var x_STORAGE "$\quad \quad$*STORAGE";
			sum $B_X;
			global B_X_hetero $B_X;
			estimates clear;
			foreach x of varlist $B_X_hetero{;
				*assign heterogeneity variable;
					des `x';
					replace x_IC =`x'_IC;
					replace x_STORAGE =`x'_STORAGE;
				*reg sep;
						local y DS_frac_sale_by_sep;
						eststo: xi: reg `y' STORAGE ICS `x' x_IC x_STORAGE
								i.stratum_rand   , cluster(communityid); 														
						summ `y' if TREAT=="Control" & e(sample);
						local y_mean_round=round(r(mean),.001);
						estadd local y_mean "`y_mean_round'";	
			};
			*Esttab;
				esttab using "tex/t_fracsale_hetero_`out_date'.tex",
				keep(ICS  STORAGE $B_X_hetero  x_IC x_STORAGE)
				order(ICS STORAGE  $B_X_hetero x_IC x_STORAGE)				
				b(%9.3f) se(%9.3f)
				scalars("y_mean Mean Y Control Group"  ) 				
				label star(* 0.1 ** 0.05 *** 0.01) replace obslast nomtitles compress longtable
				nonotes addnotes ("Notes: Robust Standard errors clustered at community level."		
				"Regression includes dummies for each randomization stratum"
							"* p$<$0.1, ** p$<$0.05, *** p$<$0.01." 							)
				nogaps			
				title(Sale Share by end of September: Heterogeneity \label{t-fracsale-hetero});									
			
	};

	**Table 7: Endline: advances and new planting;
	local run=1;
	if `run'==1{;
		estimates clear;
		foreach y in E_e_advance_2011 E_new {;
			*Reg 1;
				eststo:xi: reg `y' STORAGE ICS i.stratum_rand  , cluster(communityid); 
				summ `y' if TREAT=="Control" & e(sample);
				local y_mean_round=round(r(mean),.001);
				estadd local y_mean "`y_mean_round'";	
			*Reg 2;
			eststo: xi: reg `y' STORAGE ICS  i.stratum_rand $B_X   , cluster(communityid); 														
				summ `y' if TREAT=="Control" & e(sample);
				local y_mean_round=round(r(mean),.001);
				estadd local y_mean "`y_mean_round'";	

		};
		*Esttab;
				esttab using "tex/t_advance2011_`out_date'.tex",
				mgroups("Any Advance (0/1)" "Plant New Trees" , pattern(1 0 1 0 )
				prefix(\multicolumn{@span}{c}{) suffix(})  
				span erepeat(\cmidrule(lr){@span}))
				keep(ICS  STORAGE $B_X )
				order(ICS STORAGE  $B_X )
				b(%9.3f) se(%9.3f)
				scalars("y_mean Mean Y Control Group"  ) 				
				label star(* 0.1 ** 0.05 *** 0.01) replace obslast nomtitles compress longtable
				nonotes addnotes ("Notes: Robust Standard errors clustered at community level."
				
				"Regression includes dummies for each randomization stratum"
							"* p$<$0.1, ** p$<$0.05, *** p$<$0.01." 							)
				nogaps			
				title(Palm Oil Advance Sales and New Planting \label{t-advance2011});				
	};			

cap log close;
exit;
